package com.example.administrator.x2picturetransliteration.Home.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.Base.BaseActivity;
import com.example.administrator.x2picturetransliteration.Base.BasePresenter;
import com.example.administrator.x2picturetransliteration.Home.Presenter.Activity.ForgetPresenter;
import com.example.administrator.x2picturetransliteration.R;
import com.example.administrator.x2picturetransliteration.Util.RegularUtil;
import com.example.administrator.x2picturetransliteration.Util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 忘记密码
 * Created by Administrator on 2019/8/16.
 */

public class ForgetActivity extends BaseActivity{

    public static void startForgetActivity(Context context,String Telephone){

        Intent intent = new Intent(context,ForgetActivity.class);
        intent.putExtra("Telephone",Telephone);
        context.startActivity(intent);

    }

    public static void startForgetActivity(Context context,String Telephone,String openid,String nickname,String headPic,String registerType){

        Intent intent = new Intent(context,ForgetActivity.class);
        intent.putExtra("Telephone",Telephone);
        intent.putExtra("openid",openid);
        intent.putExtra("nickname",nickname);
        intent.putExtra("headPic",headPic);
        intent.putExtra("registerType",registerType);
        ((Activity)context).startActivityForResult(intent,SignInActivity.WEIXING);

    }

    @BindView(R.id.TitleText)TextView TitleText;
    @BindView(R.id.LButton)ImageView LButton;

    @BindView(R.id.VerificationCodeText)TextView VerificationCodeText;

    @BindView(R.id.TelephoneEdit)TextView TelephoneEdit; //手机号
    @BindView(R.id.VerificationCodeEdit)TextView VerificationCodeEdit; //验证码

    @BindView(R.id.pwView)View pwView;
    @BindView(R.id.XPWEdit)TextView XPWEdit;

    private String bd = "1";

    private ForgetPresenter mForgetPresenter;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_forget;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mForgetPresenter = new ForgetPresenter(this,mCompositeSubscriptions);

        String title = getIntent().getStringExtra("Telephone")==null?"忘记密码":"绑定手机号";

        if ("忘记密码".equals(title))pwView.setVisibility(View.VISIBLE);
        TitleText.setText(title);

        LButton.setImageResource(R.mipmap.home_backw);

    }

    private Map<String,String> map = new HashMap<>();

    /** 完成 */
    @OnClick(R.id.OkText)
    public void OkTextClick(){

        if ("".equals(TelephoneEdit.getText() + "")){

            new ToastUtil(this).getToast(false,"请输入手机号").show();
            return;

        }else if("".equals(VerificationCodeEdit.getText() + "")){

            new ToastUtil(this).getToast(false,"请输入验证码").show();
            return;

        }

        map.put("mobile",TelephoneEdit.getText() + "");
        map.put("code",VerificationCodeEdit.getText() + "");

        if ("忘记密码".equals(TitleText.getText() + "")){

            if ("".equals(XPWEdit.getText() + "")){

                new ToastUtil(this).getToast(false,"请输入密码").show();
                return;

            }else {

                if (RegularUtil.isENG_NUM(XPWEdit.getText() + "") && (XPWEdit.getText() + "").length()>=8 && (XPWEdit.getText() + "").length()<=12){

                    map.put("password",XPWEdit.getText() + "");
                    mForgetPresenter.resetPassword(map);

                }else {

                    new ToastUtil(this).getToast(false,"密码格式错误").show();

                }

            }


        }else {

            if ("1".equals(bd)){

                map.put("openid",getIntent().getStringExtra("openid"));
                map.put("nickname",getIntent().getStringExtra("nickname"));
                map.put("headPic",getIntent().getStringExtra("headPic"));
                map.put("registerType",getIntent().getStringExtra("registerType"));
                mForgetPresenter.bindMobile(map);

            }else {

                SetPwdActivity.startSetPwdActivity(ForgetActivity.this);
            }
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 2) {
            if (requestCode == 100) {
                //设置结果显示框的显示数值
                map.put("password",data.getStringExtra("pwd"));
                map.put("openid",getIntent().getStringExtra("openid"));
                map.put("nickname",getIntent().getStringExtra("nickname"));
                map.put("headPic",getIntent().getStringExtra("headPic"));
                map.put("registerType",getIntent().getStringExtra("registerType"));
                mForgetPresenter.bindMobile(map);
            }
        }
    }

    /** 获取验证码 */
    @OnClick(R.id.VerificationCodeText)
    public void VerificationCodeTextClick(){

        if (CodeJudge){

            if ("".equals(TelephoneEdit.getText() + "")){

                new ToastUtil(this).getToast(false,"请输入手机号").show();
                return;
            }

            Map<String,String> map = new HashMap<>();

            map.put("mobile",TelephoneEdit.getText() + "");

            mForgetPresenter.sendResetSmsCode(map,("忘记密码".equals(TitleText.getText() + "")), new BasePresenter.Callback() {
                @Override
                public void getData(Object data) {

                    if (data!=null)bd = data + "";
                    StyleCodeText();
                }
            });
        }

    }

    /** 返回 */
    @OnClick(R.id.left_button)
    public void left_buttonClick(){

        finish();
    }

    /** 点击获取验证码时候获取更新文本状态 */
    private volatile boolean CodeJudge = true;
    private void StyleCodeText(){

        CodeJudge = false;

        final Handler handler = new Handler();

        new Thread(new Runnable() {
            @Override
            public void run() {

                for (int i=60;i>0;i--){

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    final int finalI = i;

                    try{

                        if(VerificationCodeText==null)break;

                        handler.post(new Runnable() {
                            @Override
                            public void run() {

                                if(VerificationCodeText==null)return;

                                if (finalI ==1){

                                    VerificationCodeText.setTextColor(getResources().getColor(R.color.HomeActivityForgetTextColor1));
                                    VerificationCodeText.setText("获取验证码");
                                    CodeJudge = true;
                                }else {

                                    VerificationCodeText.setTextColor(getResources().getColor(R.color.HomeActivityForgetTextColor3));
                                    VerificationCodeText.setText(finalI + "s");
                                }
                            }
                        });

                    }catch (Exception e){

                    }
                }

            }
        }).start();
    }

}
